package com.itheima.aduit.controller;

import com.itheima.aduit.pojo.ProcInstance;
import com.itheima.aduit.pojo.ProcTaskInstance;
import com.itheima.aduit.pojo.QueryParam;
import com.itheima.aduit.service.AduitService;
import com.itheima.aduit.service.ProcessService;
import com.itheima.common.controller.BaseController;
import com.itheima.common.entity.Result;
import com.itheima.common.entity.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author qiankaofei
 * @version 1.0
 * @date 2020/12/19 17:44
 */
@RestController
@CrossOrigin
@RequestMapping("/user/process")
public class ProcessController extends BaseController {

    @Autowired
    private ProcessService processService;

    /**
     * 部署新的流程
     *      前端将绘制好的流程模型bpmn文件上传到方法中
     */
    @PostMapping("/deploy")
    public Result deployProcess(@RequestParam("file") MultipartFile file){
        try {
            processService.deployProcess(file,this.companyId);
            return new Result(ResultCode.SUCCESS);
        } catch (IOException e) {
            return new Result(ResultCode.FAIL);
        }
    }


    /**
     * 查询所有流程定义
     */
    @GetMapping("/definition")
    public Result definitionList(){
       try {
           return new Result(ResultCode.SUCCESS,processService.definitionList(this.companyId));
       }catch (Exception e){
           return new Result(ResultCode.FAIL);
       }
    }

    /**
     * 设置流程的挂起与激活状态
     *
     * 挂起状态设置为激活
     * 不是激活设置为挂起
     */
    @GetMapping("/suspend/{processKey}")
    public Result setSuspendProcess(@PathVariable("processKey") String processKey){
        try{
            processService.setSuspendProcess(processKey,this.companyId);
            return new Result(ResultCode.SUCCESS);
        }catch (Exception e){
            return new Result(ResultCode.FAIL);
        }
    }

    @Autowired
    private AduitService aduitService;

    /**
     * 根据id查询流程申请详情
     */
    @RequestMapping("/instance/{id}")
    public Result findInstanceById(@PathVariable("id") String id){
        return new Result(ResultCode.SUCCESS,aduitService.findInstanceById(id));
    }

    /**
     * 查询申请列表
     *  参数:
     *      page,size
     *  业务参数:
     *      审批类型
     *      审批状态(多个,每个状态之间使用","隔开)
     *      当前节点的待处理人
     */
    @PostMapping(value = "/instance/{page}/{size}")
    public Result instanceList(@RequestBody ProcInstance instance, @PathVariable("page") Integer page, @PathVariable("size") Integer size){
        instance.setCompanyId(this.companyId);
        return new Result(ResultCode.SUCCESS,aduitService.instanceList(page,size,instance,this.companyId));
    }


    /**
     * 流程申请
     */
    @PostMapping(value = "/startProcess")
    public Result startProcess(@RequestBody QueryParam queryParam){
        aduitService.startProcess(queryParam,this.companyId);
        return new Result(ResultCode.SUCCESS);
    }


    /**
     * 流程提交
     */
    @PostMapping
    public Result commit(@RequestBody ProcTaskInstance procTaskInstance){
        aduitService.commit(procTaskInstance,companyId);
        return new Result(ResultCode.SUCCESS);
    }
}
